home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_055 / csh / run.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  102 lines

  1.  
  2. /*
  3.  * RUN.C
  4.  *
  5.  * (c)1986 Matthew Dillon     9 October 1986
  6.  *
  7.  *    RUN   handles running of external commands.
  8.  *
  9.  * version 2.05M (Manx Version and Additions) by Steve Drew 20-Jan-87
  10.  *
  11.  */
  12.  
  13. #include "shell.h"
  14.  
  15. char *FindIt();
  16.  
  17. do_run(str)
  18. char *str;
  19. {
  20.    int i, try = -1;
  21.    int run = 0;
  22.    char buf[128];
  23.    char runcmd[128];
  24.    char *save, *path;
  25.  
  26.  
  27.    if (path = FindIt(av[0],"",buf)) {   
  28.       if (!strcmp(av[0],"run")) {
  29.          if (FindIt(av[1],"",runcmd)) {
  30.             run = 1;
  31.             save = av[1];
  32.             av[1] = runcmd;
  33.          }
  34.       }
  35.       if ((try = fexecv(path, av)) == 0)
  36.          i = wait();
  37.       if (run) av[1] = save;      
  38.    } 
  39.    if (try) {
  40.       long lock;
  41.       char *copy;
  42.  
  43.       if ((path = FindIt(av[0],".sh",buf)) == NULL) {
  44.          fprintf(stderr,"Command Not Found %s\n",av[0]);
  45.          return (-1);
  46.       }
  47.       av[1] = buf;               /* particular to do_source() */
  48.       copy = malloc(strlen(str)+3);
  49.       strcpy(copy+2,str);
  50.       copy[0] = 'x';
  51.       copy[1] = ' ';
  52.       i = do_source(copy);
  53.       free(copy);
  54.    }
  55.    return (i);
  56. }
  57.  
  58.  
  59. char *
  60. FindIt(cmd, ext, buf)
  61. char *cmd;
  62. char *ext;
  63. char *buf;
  64. {
  65.    long lock = 0;
  66.    char hasprefix = 0;
  67.    APTR original;
  68.    char *ptr, *s = NULL;
  69.  
  70.    original = Myprocess->pr_WindowPtr;
  71.  
  72.    for (ptr = cmd; *ptr; ++ptr) {
  73.       if (*ptr == '/' || *ptr == ':')
  74.          hasprefix = 1;
  75.    }
  76.    
  77.    if (!hasprefix) {
  78.        Myprocess->pr_WindowPtr = (APTR)(-1);
  79.        s = get_var(LEVEL_SET, V_PATH);
  80.    }
  81.  
  82.    strcpy(buf, cmd);
  83.    strcat(buf, ext);
  84.    while ((lock = (long)Lock(buf, ACCESS_READ)) == 0) {
  85.       if (*s == NULL || hasprefix) break;
  86.       for(ptr = s; *s && *s != ','; s++) ;
  87.       strcpy(buf, ptr);
  88.       buf[s-ptr] = '\0';
  89.       strcat(buf, cmd);
  90.       strcat(buf, ext);
  91.       if (*s) s++;
  92.    }
  93.    Myprocess->pr_WindowPtr = original;
  94.    if (lock) {
  95.       UnLock(lock);
  96.       return(buf);
  97.    }
  98.    return(NULL);
  99. }
  100.  
  101.  
  102.